package com.chem.lems.serves.impl;

import com.chem.lems.constans.ResultCodeConstant;
import com.chem.lems.entity.User;
import com.chem.lems.repository.RoleRepository;
import com.chem.lems.repository.UserRepository;
import com.chem.lems.serves.UserServies;
import com.chem.lems.utils.ResultUtil;
import com.chem.lems.vo.PassVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserServiesImpl implements UserServies {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    RoleRepository roleRepository;

    /**
     * 获取全部用户列表
     *
     * @return
     */

    public ResultUtil gitUserByAll() {
        List<User> userList = userRepository.findAll();
        Map<String, Object> userMap = new HashMap<>();
        userMap.put("users", userList);
        return new ResultUtil(ResultCodeConstant.SUCCESS, "获取用户列表成功！", userMap);
    }

    /**
     * 获取全部用户列表 分页
     *
     * @param page
     * @param size
     * @return
     */
    public ResultUtil gitUserByAll(Integer page, Integer size) {
        PageRequest pageRequest = PageRequest.of(page, size);
        Page<User> pageList = userRepository.findAll(pageRequest);
        long total = pageList.getTotalElements();
        List<User> userList = pageList.getContent();
        Map<String, Object> userMap = new HashMap<>();
        userMap.put("users", userList);
        userMap.put("total", total);
        return new ResultUtil(ResultCodeConstant.SUCCESS, "获取用户列表成功！", userMap);
    }

    /**
     * 模糊查询用户列表 分页
     *
     * @param username
     * @param page
     * @param size
     * @return
     */
    public ResultUtil gitUserByNameLike(String username, Integer page, Integer size) {
        Map<String, Object> userMap = new HashMap<>();
        long total = 0;
        List<User> userList =new ArrayList<>();
        PageRequest pageRequest = PageRequest.of(page, size);
        // 如果是32为UUID
        if (username.length()==32){
            Optional<User> userOpt = userRepository.findById(username);
            if (userOpt.isPresent()){
                User user =  userOpt.get();
                userList.add(user);
                total=1;
            }
        }else {
            Page<User> pageList = userRepository.findByAllNameLike(username, username,pageRequest);
            total = pageList.getTotalElements();
            userList = pageList.getContent();
        }
        userMap.put("users", userList);
        userMap.put("total", total);
        return new ResultUtil(ResultCodeConstant.SUCCESS, "获取用户列表成功！", userMap);
    }

    /**
     * 用户名查询用户信息
     *
     * @param username
     * @return
     */
    public ResultUtil gitUserByName(String username) {
        User result = userRepository.findByUserName(username);
        return new ResultUtil(ResultCodeConstant.SUCCESS, "获取用户信息成功！", result);
    }

    /**
     * 用户名查询用户信息
     *
     * @param userId
     * @return
     */
    public ResultUtil gitUserByUserId(String userId) {
        User result = userRepository.findById(userId).get();
        return new ResultUtil(ResultCodeConstant.SUCCESS, "获取用户信息成功！", result);
    }

    /**
     * 更新用户状态
     *
     * @param userId
     * @param state
     * @return
     */
    public ResultUtil updataUserState(String userId, boolean state) {
        User user = userRepository.findById(userId).get();
        user.setUserState(state);
        User result = userRepository.save(user);
        if (result == null) {
            return new ResultUtil(ResultCodeConstant.FAIL, "用户状态更改失败！", result);
        }
        return new ResultUtil(ResultCodeConstant.SUCCESS, "用户状态更改成功！", result);
    }

    /**
     * 修改用户信息
     *
     * @param editUser
     * @return
     */
    public ResultUtil editUserInfo(User editUser) {
        User result = userRepository.save(editUser);
        if (result == null) {
            return new ResultUtil(ResultCodeConstant.FAIL, "用户信息修改失败！", result);
        }
        return new ResultUtil(ResultCodeConstant.SUCCESS, "用户信息修改成功！", result);
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return
     */
    public ResultUtil deleteById(String userId) {
        userRepository.deleteById(userId);
        return new ResultUtil(ResultCodeConstant.SUCCESS, "用户删除成功！");
    }


    /**
     * 新增用户
     *
     * @param userFrom
     * @return
     */
    public ResultUtil addUser(User userFrom) {
        userFrom.setUserState(true);
        userFrom.setCreateTime(new Date());
        User result = userRepository.save(userFrom);
        if (result == null) {
            return new ResultUtil(ResultCodeConstant.FAIL, "用户添加失败！", result);
        }
        userRepository.saveUserRole(result.getUserId(), 3);
        return new ResultUtil(ResultCodeConstant.SUCCESS, "用户添加成功！", result);
    }

    /**
     * 给用户分配角色
     *
     * @param userId
     * @param roleId
     * @return
     */
    @Override
    public ResultUtil UserAuthRole(String userId, String roleId) {
        Integer state = userRepository.updataUserRole(roleId, userId);
        if (state == 1) {
            return new ResultUtil(ResultCodeConstant.SUCCESS, "角色分配成功！");
        }
        return new ResultUtil(ResultCodeConstant.FAIL, "角色分配失败！");
    }


    /**
     * 查询角色下所用用户信息
     *
     * @param roleId
     * @param page
     * @param size
     * @return
     */
    public ResultUtil gitstudentsListByAll(String roleId, Integer page, Integer size) {
        PageRequest pageRequest = PageRequest.of(page, size);
        Page<User> pageList = userRepository.findStudents(roleId, pageRequest);
        long total = pageList.getTotalElements();
        List<User> userList = pageList.getContent();
        Map<String, Object> userMap = new HashMap<>();
        userMap.put("users", userList);
        userMap.put("total", total);
        return new ResultUtil(ResultCodeConstant.SUCCESS, "获取用户列表成功！", userMap);
    }

    @Override
    public ResultUtil ChangePasswd(String userId, PassVO passVO) {
        User user = userRepository.findById(userId).get();
        if (passVO.getOldPass().equals(user.getPassword())) {
            user.setPassword(passVO.getPass());
            User newUser = userRepository.save(user);
            if (null!=newUser){
                return new ResultUtil(ResultCodeConstant.SUCCESS, "密码修改成功，请重新登录！");
            }
            return new ResultUtil(ResultCodeConstant.FAIL, "密码修改失败！");
        }
        return new ResultUtil(ResultCodeConstant.FAIL, "旧密码错误！");
    }

    /**
     * 模糊查询学生列表 分页
     *
     * @param username
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultUtil gitstudentsByNameLike(String username, Integer page, Integer size) {
        return null;
    }


}
